import axios from 'axios'
import qs from 'qs'
import { errorAlert } from './alert'
import store from '../store'
import router from '../router'

// 1.设置基础路径
const baseUrl = "/api";
// 设置请求拦截、
axios.interceptors.request.use(config=>{
    console.log('本次请求的地址为:'+config.url) 
     // 设置请求信息:目的config.headers.authorization = token
    console.log(config.url)

    if(config.url !== baseUrl+'/api/userlogin'){
        //在此处设置请求信息
        if(sessionStorage.getItem('user')){
          config.headers.authorization = store.state.user.token
        }else{
          router.push('/login');
          return
        }
    }
    return config
})

//2.设置响应拦截
axios.interceptors.response.use(res => {
    console.group('本次响应的地址为:' + res.config.url)
    if (res.data.code !== 200) {
        errorAlert(res.data.msg)
        return
    }
    console.log(res);
    return res
})

//添加菜单
export const addMenu = (data) => {
    return axios({
        method: 'post',
        url: baseUrl + '/api/menuadd',
        data: qs.stringify(data)
    })
}

// 获取菜单列表
export const getMenu = (params) => {
    return axios({
        method: 'get',
        url: baseUrl + '/api/menulist',
        params
    })
}

//获取菜单信息(一条)

export const oneMenu = (params) => {
    return axios({
        method: 'get',
        url: baseUrl + '/api/menuinfo',
        params
    })
}

// 菜单修改
export const updateMenu = (data) => {
    return axios({
        method: 'post',
        url: baseUrl + '/api/menuedit',
        data: qs.stringify(data)
    })
}

//菜单删除
export const delMenu = (data) => {
    return axios({
        method: 'post',
        url: baseUrl + '/api/menudelete',
        data: qs.stringify(data)
    })
}

// 角色添加
export const addRole = (data) =>{
    return axios({
        method:'post',
        url: baseUrl + '/api/roleadd',
        data:qs.stringify(data)
    })
}

//获取角色列表
export const getRole = (params) =>{
    return axios({
        method:'get',
        url: baseUrl+'/api/rolelist',
        params
    })
}

// 获取角色(一条)
export const oneRole = (params) =>{
    return axios({
        method:'get',
        url : baseUrl + '/api/roleinfo',
        params
    })
}

// 角色修改
export const updateRole =  (data) =>{
    return axios ({
        method:'post',
        url:baseUrl + '/api/roleedit',
        data:qs.stringify(data)
    })
}

//角色删除
export const delRole = (data) =>{
    return axios ({
        method:'post',
        url:baseUrl+'/api/roledelete',
        data:qs.stringify(data)
    })
}

// 管理员添加
export const addManager = (data) =>{
    return axios ({
        method:'post',
        url:baseUrl+'/api/useradd',
        data:qs.stringify(data)
    })
}


//管理员总数
export const managerCount = (params) =>{
    return axios ({
        method:'get',
        url:baseUrl+'/api/usercount',
        params
    })
}
//管理员列表

export const getManager = (params) =>{
    return axios({
        method:'get',
        url:baseUrl+'/api/userlist',
        params
    })
}

//管理员获取(一条)
export const oneManager = (params) =>{
    return axios ({
        method:'get',
        url:baseUrl+'/api/userinfo',
        params
    })
}

// 管理员修改
export const updateManager = (data) =>{
    return axios ({
        method:'post',
        url:baseUrl+'/api/useredit',
        data:qs.stringify(data)
    })
}

//管理员删除
 export const delManager = (data) =>{
     return axios ({
         method:'post',
         url:baseUrl+'/api/userdelete',
         data:qs.stringify(data)
     })
 }

 //管理员登录
 export const getLogin = (data) =>{
     return axios ({
         method:'post',
         url:baseUrl+'/api/userlogin',
         data:qs.stringify(data)
     })
 }